﻿
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using KDS.Mes.Mes.ProcessTypes.Dtos;
using KDS.Mes.Authorization;

namespace KDS.Mes.Mes.ProcessTypes
{
    /// <summary>
    /// 工序类型信息服务实现
    /// </summary>
    [AbpAuthorize(AppPermissions.Pages_Bom_ProcessTypes)]
    public class ProcessTypeAppService : MesAppServiceBase, IProcessTypeAppService
    {
        private readonly IRepository<ProcessType, int> _processTypeRepository;


        private readonly ProcessTypeManage _processTypeManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public ProcessTypeAppService(IRepository<ProcessType, int> processTypeRepository,
ProcessTypeManage processTypeManage

  )
        {
            _processTypeRepository = processTypeRepository;
            _processTypeManage = processTypeManage;

        }

        #region 工序类型信息管理

        public PagedResultDto<ProcessTypeListDto> GetPagedProcessTypes(GetProcessTypeInput input)
        {
            //初步过滤
            var query = _processTypeRepository.GetAll();
            //排序
            query = !string.IsNullOrEmpty(input.Sorting) ? query.OrderBy(x => x.Id) : query.OrderByDescending(t => t.CreationTime);
            //获取总数
            var tasksCount = query.Count();
            //默认的分页方式
            //var taskList = query.Skip(input.SkipCount).Take(input.MaxResultCount).ToList();
            //ABP提供了扩展方法PageBy分页方式
            var taskList = query.PageBy(input).ToList();
            return new PagedResultDto<ProcessTypeListDto>(tasksCount, taskList.MapTo<List<ProcessTypeListDto>>());
        }

        /// <summary>
        /// 根据查询条件获取工序类型信息分页列表
        /// </summary>
        public async Task<PagedResultDto<ProcessTypeListDto>> GetPagedProcessTypesAsync(GetProcessTypeInput input)
        {

            var query = _processTypeRepository.GetAll();
            //TODO:根据传入的参数添加过滤条件

            var processTypeCount = await query.CountAsync();

            var processTypes = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var processTypeListDtos = processTypes.MapTo<List<ProcessTypeListDto>>();
            return new PagedResultDto<ProcessTypeListDto>(
            processTypeCount,
            processTypeListDtos
            );
        }

        /// <summary>
        /// 通过Id获取工序类型信息信息进行编辑或修改 
        /// </summary>
        public async Task<GetProcessTypeForEditOutput> GetProcessTypeForEditAsync(NullableIdDto<int> input)
        {
            var output = new GetProcessTypeForEditOutput();

            ProcessTypeEditDto processTypeEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _processTypeRepository.GetAsync(input.Id.Value);
                processTypeEditDto = entity.MapTo<ProcessTypeEditDto>();
            }
            else
            {
                processTypeEditDto = new ProcessTypeEditDto();
            }

            output.ProcessType = processTypeEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取工序类型信息ListDto信息
        /// </summary>
        public async Task<ProcessTypeListDto> GetProcessTypeByIdAsync(EntityDto<int> input)
        {
            var entity = await _processTypeRepository.GetAsync(input.Id);

            return entity.MapTo<ProcessTypeListDto>();
        }







        /// <summary>
        /// 新增或更改工序类型信息
        /// </summary>
        public async Task CreateOrUpdateProcessTypeAsync(CreateOrUpdateProcessTypeInput input)
        {
            if (input.ProcessTypeEditDto.Id.HasValue)
            {
                await UpdateProcessTypeAsync(input.ProcessTypeEditDto);
            }
            else
            {
                await CreateProcessTypeAsync(input.ProcessTypeEditDto);
            }
        }

        /// <summary>
        /// 新增工序类型信息
        /// </summary>
        [AbpAuthorize(AppPermissions.Pages_Bom_ProcessTypes_Create)]
        public virtual async Task<ProcessTypeEditDto> CreateProcessTypeAsync(ProcessTypeEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<ProcessType>();

            entity = await _processTypeRepository.InsertAsync(entity);
            return entity.MapTo<ProcessTypeEditDto>();
        }

        /// <summary>
        /// 编辑工序类型信息
        /// </summary>
        [AbpAuthorize(AppPermissions.Pages_Bom_ProcessTypes_Edit)]
        public virtual async Task UpdateProcessTypeAsync(ProcessTypeEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _processTypeRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _processTypeRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除工序类型信息
        /// </summary>
        [AbpAuthorize(AppPermissions.Pages_Bom_ProcessTypes_Delete)]
        public async Task DeleteProcessTypeAsync(EntityDto<int> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _processTypeRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除工序类型信息
        /// </summary>
        [AbpAuthorize(AppPermissions.Pages_Bom_ProcessTypes_Delete)]
        public async Task BatchDeleteProcessTypeAsync(List<int> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _processTypeRepository.DeleteAsync(s => input.Contains(s.Id));
        }

        #endregion












    }
}
